﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies.Takenlijst_windows
{
    public partial class Addlid : UserControl
    {
        //projectID uit vorige scherm doorgestuurd
        private int projectID;
        //lijst met alle projectleden (IDs)
        private string[] idList = new string[10];
        //hashtable voor users, voorkomt het gebruik van een extra query
        Hashtable users = new Hashtable();
        //hashtable voor de taken en de ids
        Hashtable taken = new Hashtable();
        //counter voor de IDs
        private int counter = 0;

        MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");

        public Addlid()
        {

        }

        public Addlid(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
            getIds();
        }

        private void getIds()
        {
            try
            {
                MySqlCommand retrieveIds = new MySqlCommand("SELECT UserID FROM project_members WHERE ProjectID='"+projectID+"'", grabConnection);
                grabConnection.Open();
                MySqlDataReader getIds = retrieveIds.ExecuteReader();

                while (getIds.Read())
                {
                    if (getIds["UserID"].ToString() == null)
                    {
                        break;
                    }

                    idList[counter] = getIds["UserID"].ToString();
                    counter++;
                }
                //objecten sluiten en counter resetten
                getIds.Close();
                counter = 0;
                grabConnection.Close();

                //IDs omzetten naar namen
                if (idList[0] != null)
                {
                    getNames();
                }
                else
                {
                    MessageBox.Show("Foutmelding: er kunnen geen Ids opgehaald worden");
                }
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void getNames()
        {
            grabConnection.Open();

            for (int i = 0; i <= idList.Length; i++)
            {
                if (idList[i] == null)
                {
                    break;
                }
                
                MySqlCommand retrieveUsers = new MySqlCommand("SELECT Naam FROM confirmed_users WHERE UserID='"+idList[i]+"'", grabConnection);
                MySqlDataReader getNames = retrieveUsers.ExecuteReader();

                while (getNames.Read())
                {
                    comboBox1.Items.Add(getNames["Naam"].ToString());
                    users.Add(getNames["Naam"].ToString(), idList[i]);
                }

                getNames.Close();
            }

            grabConnection.Close();
            getTasks();
        }

        private void getTasks()
        {
            MySqlCommand takenGet = new MySqlCommand("SELECT TaakID, Taak FROM taken WHERE ProjectID='"+projectID+"'", grabConnection);

            grabConnection.Open();

            MySqlDataReader reader = takenGet.ExecuteReader();
            
            if (reader.Read() != false)
            {
                if (reader.IsDBNull(0) == true)
                {
                    grabConnection.Close();
                    reader.Dispose();
                    takenGet.Dispose();
                }
                else
                {
                    while (reader.Read())
                    {
                        comboBox2.Items.Add(reader["Taak"].ToString());
                        taken.Add(reader["Taak"].ToString(), reader["TaakID"].ToString());
                    }

                    reader.Close();
                }

                grabConnection.Close();
                reader.Dispose();
                takenGet.Dispose();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string user = comboBox1.SelectedItem.ToString();
            string taak = comboBox2.SelectedItem.ToString();

            string userID = users[user].ToString();
            string taakID = taken[taak].ToString();

            if (user != null && taak != null)
            {
                MySqlCommand attachTask = new MySqlCommand("INSERT INTO taak_lid (TaakID, UserID)" + "VALUES ('"+taakID+"','"+userID+"')", grabConnection);
                try
                {
                    grabConnection.Open();
                    attachTask.ExecuteNonQuery();
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    grabConnection.Close();
                    this.Controls.Clear();
                    this.Controls.Add(new Takenlijst(projectID));
                }
            }
        }
    }
}
